﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PNProject.Core;
using PNProject.Core.Converter;
using PNProject.EF;
using System.Web.Security;

namespace PNProject.Controllers
{
    [Authorize]
    public class AdminFeedbackController : Controller
    {
        //
        // GET: /AdminFeedback/

        public ActionResult Index(string page)
        {
            int value = 0 ;
            //try { value = int.Parse(page); }
            //catch (Exception e) { value = 0; }
            URLPage pageModel = new URLPage() { Page = value.ToString() };
            return View(pageModel);
        }

        public ActionResult NewFeedback()
        {
            var newfeed = StaticData.feedbackRepo.GetSome(a => (a.IsRead == null || a.IsRead == false)).OrderByDescending(a=>a.SentDate);
            List<FeedbackModel> listFeed = new List<FeedbackModel>();
            foreach (var item in newfeed)
            {
                var con = FeedbackConverter.FeedbackTo(item);
                listFeed.Add(con);
            }
            return View(listFeed);
        }

        public ActionResult ListFeedback(string id)
        {
            PagingFeedback pagemodel = new PagingFeedback();
            List<Feedback> dboQurey = new List<Feedback>();
            var count = StaticData.feedbackRepo.GetSome(a => a.IsRead == true).ToList().Count;
            pagemodel.TotalItem = count;
            if (string.IsNullOrEmpty(id))
            {
                pagemodel.ActivePage = 1;
                dboQurey = StaticData.feedbackRepo.GetSome(a => a.IsRead == true, Information.NumberOfItemOnAdminPage, 0).ToList();
            }
            else
            {
                int page = 0;
                try { page = int.Parse(id); }
                catch (Exception e) { }
                dboQurey = StaticData.feedbackRepo.GetSome(a =>  a.IsRead == true, Information.NumberOfItemOnAdminPage, (page -1) * Information.NumberOfItemOnAdminPage).ToList();
                pagemodel.ActivePage = page;
            }

            pagemodel.ListPages = PNLibraries.GetShowedPage(count, pagemodel.ActivePage, 10, 5);
            pagemodel.Content = new List<FeedbackModel>();
            List<FeedbackModel> listFeed = new List<FeedbackModel>();
            foreach (var item in dboQurey)
            {
                var con = FeedbackConverter.FeedbackTo(item);
                listFeed.Add(con);
            }
            pagemodel.Content = listFeed;
            return View(pagemodel);
        }

        public ActionResult Reply(string id)
        {
            try
            {
                Guid conid = Guid.Parse(id);
                var dbo = StaticData.feedbackRepo.GetOne(a => a.FeedbackID == conid);
                var convert = FeedbackConverter.FeedbackTo(dbo);
                return View(convert);
            }
            catch(Exception e)
            {
                return RedirectToAction("Error","Admin");
            }
            
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Reply(FeedbackModel model)
        {
            try
            {
                Guid newid = Guid.Parse(model.FeedbackID);
                var dbo = StaticData.feedbackRepo.GetOne(a => a.FeedbackID == newid);
                if (dbo != null)
                {
                    var copy = FeedbackConverter.CopyFeedback(dbo);
                    copy.ReplyMessage = model.ReplyMessage;
                    copy.ReplyDate = DateTime.Now;
                    copy.ReplyBy = Membership.GetUser().UserName;
                    copy.IsRead = true;
                    copy.Title = model.Title;
                    copy.Message = model.Message;
                    copy.Phone = model.Phone;
                    copy.Address = model.Address;
                    copy.Name = model.Name;
                    copy.Lang = model.Lang;
                    StaticData.feedbackRepo.Save(dbo, copy);
                    //StaticData.SendEmail()
                    
                }
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                return RedirectToAction("Error", "Admin");
            }
        }

        public ActionResult Delete(string id)
        {
            try
            {
                Guid conid = Guid.Parse(id);
                var dbo = StaticData.feedbackRepo.GetOne(a => a.FeedbackID == conid);
                if (dbo != null)
                {
                    StaticData.feedbackRepo.Delete(dbo);
                }
            }
            catch (Exception e)
            {
            }
            return RedirectToAction("Index");
            return View();
        }

        public ActionResult View(string id)
        {
            try
            {
                Guid conid = Guid.Parse(id);
                var dbo = StaticData.feedbackRepo.GetOne(a => a.FeedbackID == conid);
                var copy = FeedbackConverter.CopyFeedback(dbo);
                copy.IsRead = true;
                StaticData.feedbackRepo.Save(dbo, copy);
                var convert = FeedbackConverter.FeedbackTo(dbo);
                return View(convert);
            }
            catch (Exception e)
            {
                return RedirectToAction("Error", "Admin");
            }
        }

        public ActionResult Change(string id)
        {
            try
            {
                Guid conid = Guid.Parse(id);
                var dbo = StaticData.feedbackRepo.GetOne(a => a.FeedbackID == conid);
                var copy = FeedbackConverter.CopyFeedback(dbo);
                var flag = copy.Type.HasValue?copy.Type.Value:0;
                copy.Type = flag == 0 ? 1 : 0;
                StaticData.feedbackRepo.Save(dbo, copy);
                return RedirectToAction("View", "AdminFeedback", new { id = id });
            }
            catch (Exception e)
            {
                return RedirectToAction("Error", "Admin");
            }
        }

        public ActionResult Questions(string id)
        {
            PagingFeedback pagemodel = new PagingFeedback();
            List<Feedback> dboQurey = new List<Feedback>();
            var count = StaticData.feedbackRepo.GetSome(a => a.IsRead == true && a.Type == 1).ToList().Count;
            pagemodel.TotalItem = count;
            if (string.IsNullOrEmpty(id))
            {
                pagemodel.ActivePage = 1;
                dboQurey = StaticData.feedbackRepo.GetSome(a => a.IsRead == true && a.Type == 1, Information.NumberOfItemOnAdminPage, 0).ToList();
            }
            else
            {
                int page = 0;
                try { page = int.Parse(id); }
                catch (Exception e) { }
                dboQurey = StaticData.feedbackRepo.GetSome(a => a.IsRead == true && a.Type == 1, Information.NumberOfItemOnAdminPage, (page - 1) * Information.NumberOfItemOnAdminPage).ToList();
                pagemodel.ActivePage = page;
            }

            pagemodel.ListPages = PNLibraries.GetShowedPage(count, pagemodel.ActivePage, 10, 5);
            pagemodel.Content = new List<FeedbackModel>();
            List<FeedbackModel> listFeed = new List<FeedbackModel>();
            foreach (var item in dboQurey)
            {
                var con = FeedbackConverter.FeedbackTo(item);
                listFeed.Add(con);
            }
            pagemodel.Content = listFeed;
            return View(pagemodel);
        }

    }
}
